package com.pengcheng.taskapplication.mvp.view.activity;

import android.view.View;
import android.widget.TextView;

import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.adapter.DragSortAdapter;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.PayStatusModel;
import com.pengcheng.taskapplication.model.SortModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.mvp.view.widget.DragSortListView;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class PaySortActivity extends BaseActivity implements IView {

    @BindView(R.id.tv_center_title)
    TextView tv_center_title;

    @BindView(R.id.activity_dragsortlistview_listview)
    DragSortListView dragSortListView;

    private DragSortAdapter dragSortAdapter;
    UserPresenterV2 userPresenterV2;

    ArrayList<SortModel> list=new ArrayList<>();
    private String uid;
    private String newtoken;

    @Override
    protected int layoutResID() {
        return R.layout.activity_pay_sort_list;
    }

    @Override
    protected void initData() {
        tv_center_title.setText("扣款顺序");
        userPresenterV2=new UserPresenterV2(this);
        uid = SPUtils.getString(context, ConstantValue.SpType.userid);
        newtoken = SPUtils.getString(context, ConstantValue.SpType.newtoken);
        dragSortListView.setDropListener(onDrop);
        dragSortAdapter=new DragSortAdapter(PaySortActivity.this,list);
        dragSortListView.setAdapter(dragSortAdapter);
        dragSortListView.setDragEnabled(true); // 设置是否可拖动。
        getPaySort(newtoken,uid);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.getPaySort)){
            PayStatusModel model=(PayStatusModel)data;
            if(model!=null){
                if(model.getPaywaylist()!=null){
                    list.clear();
                    list.addAll(model.getPaywaylist());
                    dragSortAdapter.notifyDataSetChanged();
                }
            }

        }
        if(actionType.equals(ConstantValue.RequestKey.setPaySort)){
            ToastUtils.showShortToast("修改成功");
        }
    }

    @Override
    public void onFailed(String code, String msg) {

    }
    // 监听器在手机拖动停下的时候触发
    private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
        @Override
        public void drop(int from, int to) {// from to 分别表示 被拖动控件原位置 和目标位置
            if (from != to) {
                SortModel item = (SortModel) dragSortAdapter.getItem(from);// 得到listview的适配器
                dragSortAdapter.remove(from);// 在适配器中”原位置“的数据。
                dragSortAdapter.insert(item, to);// 在目标位置中插入被拖动的控件。

                ArrayList<SortModel> list=dragSortAdapter.getList();
                StringBuffer stringBuffer=new StringBuffer();
                for(SortModel sortModel:list){
                    stringBuffer.append(sortModel.getId()+",");
                }
                String payOrder=stringBuffer.toString();
                payOrder = payOrder.substring(0, payOrder.length() -1);

                setPaySort(newtoken,uid,payOrder);
            }
        }
    };

    public void getPaySort(String token,String uid){
        userPresenterV2.getPaySort(token,uid,"0", ConstantValue.RequestKey.getPaySort);
    }
    public void setPaySort(String token,String uid,String payorder){
        userPresenterV2.setPaySort(token,uid,payorder, ConstantValue.RequestKey.setPaySort);
    }




    @OnClick({R.id.img_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
        }
    }
}
